//QQ互联接口
var QueryString = require("querystring");
var Config = require("../config");
var KOC = require("koc.common.utils");

var _Interface_QQConnect = module.exports = {
  _RedirectURI: Config.Common.Virtual_Path.Main + "oauth/qqconnect.token",
  /////////////////////////////////////////
  //取得登录地址
  /////////////////////////////////////////
  OAuth_LoginUrl: function () {
    return "https://graph.qq.com/oauth2.0/authorize?response_type=code" +
      "&client_id=" + Config.Interface.QQContent.APPID +
      "&redirect_uri=" + _Interface_QQConnect._RedirectURI +
      "&scope=get_user_info,get_info" +
      "&state=test";
  },
  /////////////////////////////////////////
  //获取AccessToken
  /////////////////////////////////////////
  OAuth_Token: function (code, callback) {
    if (typeof callback !== "function") {
      return;
    }
    var options = {
      uri: "https://graph.qq.com/oauth2.0/token",
      method: "GET",
      qs: {
        grant_type: "authorization_code",
        client_id: Config.Interface.QQContent.APPID,
        client_secret: Config.Interface.QQContent.APPKey,
        code: code,
        redirect_uri: _Interface_QQConnect._RedirectURI
      }
    };
    KOC.Request(options, function (error, response, result) {
      if (error) {
        callback(KOC.ReturnValue.New({
          hasError: true,
          message: error.message
        }));
        return;
      }
      // access_token=FE04************************CCE2&expires_in=7776000&refresh_token=88E4************************BE14
      result = QueryString.parse(result);
      if (!result.access_token) {
        callback(KOC.ReturnValue.New({
          hasError: true,
          message: result.msg + result.code
        }));
        return;
      }
      callback(KOC.ReturnValue.New({
        message: "",
        returnObject: {
          AccessToken: result.access_token,
          ExpiresIn: result.expires_in,
          RefreshToken: result.refresh_token
        }
      }));
    });
  },
  /////////////////////////////////////////
  //获取OpenID
  /////////////////////////////////////////
  OAuth_OpenID: function (token, callback) {
    if (typeof callback !== "function") {
      return;
    }
    var options = {
      uri: "https://graph.qq.com/oauth2.0/me",
      method: "GET",
      qs: {
        access_token: token
      }
    };
    KOC.Request(options, function (error, response, result) {
      if (error) {
        callback(KOC.ReturnValue.New({
          hasError: true,
          message: error.message
        }));
        return;
      }
      try {
        result = JSON.parse(result.replace('callback( ', '').replace(' );', ''));
      } catch (ex) {
        callback(KOC.ReturnValue.New({
          hasError: true,
          message: result.msg + result.code
        }));
        return;
      }
      if (!result.openid) {
        callback(KOC.ReturnValue.New({
          hasError: true,
          message: result.msg + result.code
        }));
        return;
      }
      callback(KOC.ReturnValue.New({
        message: "",
        returnObject: {
          OpenID: result.openid,
          ClientId: result.client_id
        }
      }));
    });
  },
  /////////////////////////////////////////
  //获取用户信息
  /////////////////////////////////////////
  Get_User_Info: function (token, openid, callback) {
    var options = {
      uri: "https://graph.qq.com/user/get_user_info",
      method: "GET",
      qs: {
        access_token: token,
        oauth_consumer_key: Config.Interface.QQContent.APPID,
        openid: openid
      }
    };
    KOC.Request(options, function (error, response, result) {
      if (error) {
        callback(KOC.ReturnValue.New({
          hasError: true,
          message: error.message
        }));
        return;
      }
      result = JSON.parse(result);
      if (!result || result.ret != 0) {
        callback(KOC.ReturnValue.New({
          hasError: true,
          message: result.msg || ""
        }));
        return;
      }
      callback(KOC.ReturnValue.New({
        message: "",
        returnObject: result
      }));
    });
  }
};